﻿using System;
using Majoopic.DataAccess.Service;
using Majoopic.Utility;

namespace Majoopic.DataAccess
{
    /// <summary>
    /// Type of the factory available in Majoopic
    /// </summary>
    public enum LibraryFactoryType
    {
        /// <summary>
        /// Factory to access the library in a SQL database
        /// </summary>
        SQLAccessFactory = 1,

        /// <summary>
        /// Factory to access the library in a xml file
        /// </summary>
        XmlFileAccessFactory = 2
    }

    /// <summary>
    /// Abstract factory to access to the picture library services
    /// </summary>
    public abstract class DataAccessAbstractFactory
    {
        #region Properties
        /// <summary>
        /// Current configuration of the application
        /// </summary>
        public DataAccessConfiguration Configuration
        {
            get;
            set;
        }
        #endregion

        #region Static Methods
        /// <summary>
        /// Create the instance of the factory for the different purpose
        /// </summary>
        /// <param name="factoryType">The factory we want to use</param>
        /// <returns>The instance of the factory</returns>
        public static DataAccessAbstractFactory CreateFactory(LibraryFactoryType factoryType)
        {
            DataAccessAbstractFactory factory = null;
            switch(factoryType)
            {
                case LibraryFactoryType.SQLAccessFactory:
                    factory = FactoryLoadUtility.LoadFactory<DataAccessAbstractFactory>("Majoopic.DataAccess.SQL.dll", "Majoopic.DataAccess.SQL.DataAccessSQLFactory");
                    break;
                case LibraryFactoryType.XmlFileAccessFactory:
                    factory = FactoryLoadUtility.LoadFactory<DataAccessAbstractFactory>("Majoopic.DataAccess.XmlFile.dll", "Majoopic.DataAccess.XmlFile.DataAccessXmlFileFactory");
                    break;
                default :
                    throw new ArgumentException("The factoryType is not valid");
            }
            return factory;
        }

        /// <summary>
        /// Create the instance of the factory for the different purpose
        /// </summary>
        /// <param name="configuration">The factory we want to use</param>
        /// <returns>The instance of the factory</returns>
        public static DataAccessAbstractFactory CreateFactory(DataAccessConfiguration configuration)
        {
            DataAccessAbstractFactory factory = CreateFactory(configuration.CurrentFactoryType);
            if (factory != null)
            {
                factory.Configuration = configuration;
            }
            return factory;
        }
        #endregion

        #region Abstract Methods
        /// <summary>
        /// Get the picture service
        /// </summary>
        public abstract IPictureAccess GetPictureService();
        #endregion
    }
}
